{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a49979a2-39e8-4dde-a89e-c1cc6647e416",
   "metadata": {},
   "source": [
    "# Computation Basis State Preparation\n",
    "\n",
    "The `prepare_int` and `inplace_prepare_int` functions create a single state computational basis state, indexed by the given integer argument.\n",
    "\n",
    "The `prepare_int` will allocate a `QArray` with the minimal required size for representing the given integer argument.\n",
    "`inplace_prepare_int` on the other hand will prepare the state on a given `QArray`. It is more suitable were on wants to prepare a state with a given size."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b1f345b1-a938-49e7-8a53-8984877c32fd",
   "metadata": {},
   "source": [
    "Function: `prepare_int`\n",
    "    \n",
    "Arguments:\n",
    "\n",
    "- `value: CInt` - The index of the state to prepare in the computational basis.\n",
    "- `target: Output[QArray[QBit]]`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34b0c6a8-980d-45bc-8de6-e8c2eb748abf",
   "metadata": {},
   "source": [
    "Function: `inplace_prepare_int`\n",
    "\n",
    "Arguments:\n",
    "\n",
    "- `value: CInt` - The index of the state to prepare in the computational basis.\n",
    "- `target: QArray[QBit]` - The quantum array to prepare the state at. Should be of size at least $\\lceil \\log_2{value}\\rceil$."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "08bbf4a2-8c66-4d47-af65-3c74fe02020d",
   "metadata": {},
   "source": [
    "## Example 1\n",
    "\n",
    "Prepare the $|3\\rangle$ state (will result in a 2 qubits state space):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c5a5a8ec-c036-4554-a64b-4f27a1154714",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:26:21.731898Z",
     "iopub.status.busy": "2024-05-07T13:26:21.731421Z",
     "iopub.status.idle": "2024-05-07T13:26:24.484373Z",
     "shell.execute_reply": "2024-05-07T13:26:24.483593Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import Output, QArray, QBit, create_model, prepare_int, qfunc\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(x: Output[QArray[QBit]]):\n",
    "    prepare_int(3, x)\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f7652296-b021-4bb0-bff2-2e833df1c629",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:26:24.490201Z",
     "iopub.status.busy": "2024-05-07T13:26:24.488615Z",
     "iopub.status.idle": "2024-05-07T13:26:27.052836Z",
     "shell.execute_reply": "2024-05-07T13:26:27.052162Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from classiq import synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"prepare_int\")\n",
    "qprog = synthesize(qmod)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6feda775-c958-4b84-ba74-38aa035643b3",
   "metadata": {},
   "source": [
    "## Example 2\n",
    "\n",
    "Prepare the $|3\\rangle$ state, in a state space of 5 qubits:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ee6eea2f-11e5-45c0-9ef0-296fca5e3f89",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:26:27.059796Z",
     "iopub.status.busy": "2024-05-07T13:26:27.058607Z",
     "iopub.status.idle": "2024-05-07T13:26:27.076041Z",
     "shell.execute_reply": "2024-05-07T13:26:27.075131Z"
    }
   },
   "outputs": [],
   "source": [
    "from classiq import (\n",
    "    Output,\n",
    "    QArray,\n",
    "    QBit,\n",
    "    allocate,\n",
    "    create_model,\n",
    "    inplace_prepare_int,\n",
    "    qfunc,\n",
    ")\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(x: Output[QArray[QBit]]):\n",
    "    allocate(5, x)\n",
    "    inplace_prepare_int(3, x)\n",
    "\n",
    "\n",
    "qmod = create_model(main)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9e647b43-85d1-43e7-8613-cdfec105172f",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:26:27.081406Z",
     "iopub.status.busy": "2024-05-07T13:26:27.080207Z",
     "iopub.status.idle": "2024-05-07T13:26:29.264320Z",
     "shell.execute_reply": "2024-05-07T13:26:29.263511Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from classiq import synthesize, write_qmod\n",
    "\n",
    "write_qmod(qmod, \"inplace_prepare_int\")\n",
    "qprog = synthesize(qmod)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
